package cn.jas.common.jascommon.util;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @program:
 * @description: 文档工具类 doc  xls  xlsx
 * @author: chen_mx
 **/
//@Slf4j
public class DocumentUtils {
    private static  Logger  log= LoggerFactory.getLogger(DocumentUtils.class);
    private static final int wdFormatPDF = 17;// PDF 格式

  /*  *//** window 下转换pdf *//*
    public static void wordToPDF(String resourceFile,String  targetFile){

        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();

           *//* //转换前的文件路径
            String startFile = resourceFile;
            //转换后的文件路劲
            String overFile =  "F:\\新建文件夹\\我是转换后的pdf文件"  + ".pdf";*//*

            doc = Dispatch.call(docs,  "Open" , resourceFile).toDispatch();
            File tofile = new File(targetFile);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc,"SaveAs", targetFile, wdFormatPDF);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            Dispatch.call(doc,"Close",false);
            if (app != null)
                app.invoke("Quit", new Variant[] {});
        }
        //结束后关闭进程
        ComThread.Release();
    }*/
    /**
     *  resource  文件路径  /user/file/test.docx
     *  targetPath   /user/file/test.pdf
     *  root  libreoOffice  root 目录
     * **/
    public  static  void   linuxWord2Pdf(String resourceFile,String  targetFile ,String  sofficeDir) throws FileNotFoundException {
        if (!new File(resourceFile).exists()) {
            throw new FileNotFoundException();
        }
        String command = String.format("%s/soffice --convert-to pdf:writer_pdf_Export %s --outdir %s", sofficeDir, resourceFile, targetFile);
        String output = null;
        try {
            output = executeCommand(command);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("exec command:[{}]\noutput: [{}]", command, output);

    }

    protected static String executeCommand(String command) throws IOException, InterruptedException {
        StringBuffer output = new StringBuffer();
        Process p;
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        try (
                InputStreamReader inputStreamReader = new InputStreamReader(p.getInputStream(), "UTF-8");
                BufferedReader reader = new BufferedReader(inputStreamReader)
        ) {
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }
        }
        p.destroy();
        return output.toString();
    }


        public static void main(String[] args)  {
             String  resourceFile="D:\\data\\file\\upload\\2019-05-19\\2019-05-19fde02e7d-7e0f-456e-a5d5-4e0a29f79502.docx";
             String  targetFile="D:\\data\\test.pdf";
            // wordToPDF(resourceFile,targetFile);
            System.out.println("转换完成");
        }
}
